﻿using System.Windows.Forms;
using IGM.DataProvider;
using IGM.Framework.Constants;

namespace IGM.Presentation.Main
{
    public partial class Login : Form
    {
        private bool _isValidate;
        private readonly Business.Business _business;
        public Login()
        {
            InitializeComponent();
            _isValidate = false;
            _business = Main.Instance.IgmBusiness;
        }

        private void BtnLoginClick(object sender, System.EventArgs e)
        {
            var user = new User
            {
                Name = txtUsername.Text.Trim(),
                Password = txtPassword.Text.Trim(),

            };

            var status = _business.UserValidator(user);
            if (status < 0)
            {
                lblStatus.Text = string.Format("Đăng nhập thất bại");
            }
            else
            {
                lblStatus.Text = string.Format("Đăng nhập thành công");
                _isValidate = true;
                Dispose();

                // Phân menu theo quyền
                user.Permission = _business.GetUserPermission(user);
                Main.Instance.SetPermission(user);
            }
        }

        private void LoginFormClosing(object sender, FormClosingEventArgs e)
        {
            if (!_isValidate)
            {
                Application.Exit();
            }
        }

        private void BtnCancelClick(object sender, System.EventArgs e)
        {
            Dispose();
        }
    }
}
